<?php
/**
 * XPHP Framework
 *
 * LICENSE
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * @category	XPHP
 * @package		XPHP_View
 * @author		XWEB Dev Team
 * @copyright	Copyright (c) 2010-2011 XWEB. (http://xweb.vn)
 * @license		http://xphp.xweb.vn/license.html     GNU GPL License
 * @version		$Id: View.php 20109 2011-22-08 02:05:09 Mr.UBKey $
 */
/**
 * XPHP_Model.
 *
 * @see XPHP_Model
 */
require_once 'XPHP/Model.php';
/**
 * XPHP_Registry
 * @see XPHP_Registry
 */
require_once 'XPHP/Registry.php';
/**
 * XPHP_View
 *
 * @category	XPHP
 * @package		XPHP_View
 * @author		Mr.UBKey
 * @link		http://xphp.xweb.vn/user_guide/xphp_view.html
 */
class XPHP_View
{
    /**
     * Router lưu thông tin về area, controller, action
     * @var XPHP_Router
     */
    protected $_router;
    /**
     * Tên thư mục chứa view
     * @var string
     */
    protected $_viewDir = "Views";
    /**
     * Phần đuôi mở rộng của View
     */
    protected $_viewExt = "phtml";
    /**
     * Model trả về từ controller
     * @var XPHP_Model
     */
    protected $_model;
    /**
     * Dữ liệu chuyển từ controller sang view
     * @var stdClass
     */
    public $viewData;
    /**
     * Kết quả trả về từ controller
     */
    protected $_controllerResult;
    /**
     * Layout của view
     * @var XPHP_Layout
     */
    protected $_layout;
    /**
     * Lớp hỗ trợ view
     * @var XPHP_View_Helper
     */
    public $_helper;
    /**
     * Lớp khởi tạo View
     * @param XPHP_Router $router
     */
    public function __construct ($router)
    {
        //Load cấu hình trong file config với node view
        require_once 'XPHP/Config.php';
        XPHP_Config::load($this->viewData, 'view > viewData');
        $this->_router = $router; // Gán Router
        //Khởi tạo Layout
        $this->_layout = new XPHP_Layout($router, $this);
        //Khởi tạo lớp hỗ trợ
        $this->_helper = new XPHP_View_Helper();
    }
    public function getDefaultViewPath ()
    {
        //Lấy ra area trong router
        $area = $this->_router->getArea();
        //Lấy ra đường dẫn đến thư mục
        $folder = $this->getViewFolder();
        //Đường dẫn tới thư mục
        $mobilePath = $this->getMobileViewPath();
        //Nếu có area
        if (! empty($area)) {
            if ($mobilePath)
                $path = $mobilePath;
            else
                $path = $folder . '/' . $this->_router->getAction() . '.' .
                 $this->_viewExt;
                 //Xử lý trả về kết quả
            if (is_file($path))
                return $path;
            else {
                require_once 'XPHP/Exception.php';
                throw new XPHP_Exception(
                "Không tìm thấy tệp tin View $path của " .
                 $this->_router->getArea() . $this->_router->getAreaKey() . "_" .
                 $this->_router->getController() .
                 $this->_router->getControllerKey());
            }
        } else {
            //Nếu không có area
            if ($mobilePath)
                $path = $mobilePath;
            else
                $path = $folder . '/' . $this->_router->getAction() . '.' .
                 $this->_viewExt;
                 //Xử lý trả về kết quả
            if (is_file($path))
                return $path;
            else {
                require_once 'XPHP/Exception.php';
                throw new XPHP_Exception(
                "Không tìm thấy tệp tin View $path của " .
                 $this->_router->getController() .
                 $this->_router->getControllerKey());
            }
        }
    }
    public function getViewFolder ()
    {
        //Lấy ra area trong router
        $area = $this->_router->getArea();
        if (! empty($area)) {
            $folder = $this->_router->getAreaPaths($this->_router->getArea()) .
             '/' . $this->_viewDir . '/' . $this->_router->getController();
        } else {
            $folder = $this->_router->getApplicationDir() . '/' . $this->_viewDir .
             '/' . $this->_router->getController();
        }
        return $folder;
    }
    public function ExecuteControllerResult ()
    {
        //Gán View và Layout cho XPHP_View_Result
        if ($this->_controllerResult instanceof XPHP_View_Result) {
            $this->_controllerResult->setView($this);
            $this->_controllerResult->setLayout($this->_layout);
            $this->_controllerResult->setModel($this->_model);
            $this->_controllerResult->setHelper($this->_helper);
        }
        $this->_controllerResult->ExecuteResult();
    }
    public function setViewData ($data, $override = false)
    {
        if ($override)
            $this->viewData = $data;
        else {
            foreach ($data as $key => $value) {
                $this->viewData->$key = $value;
            }
        }
    }
    public function getViewData ()
    {
        return $this->viewData;
    }
    public function setControllerResult (XPHP_View_Result_Interface $result)
    {
        $this->_controllerResult = $result;
    }
    public function getControllerResult ()
    {
        return $this->_controllerResult;
    }
    public function setLayout ($layout)
    {
        $this->_layout = $layout;
    }
    public function getLayout ()
    {
        return $this->_layout;
    }
    public function getModel ()
    {
        return $this->_model;
    }
    public function setModel ($model)
    {
        $this->_model = $model;
    }
    public function getRouter ()
    {
        return $this->_router;
    }
    /**
     * Lấy ra đường dẫn đến file View của Mobile
     * @return mixed string | boolean Trả về chuỗi hoặc false nếu không tìm thấy file mobile view
     */
    public function getMobileViewPath ()
    {
        //Lấy ra folder
        $folder = $this->getViewFolder();
        //Lấy ra Application từ Registry và kiểm tra có detect hay không?
        $application = XPHP_Registry::get("Application");
        if ($application->mode == "mobile") {
            //Mobile path
            $mobilePath = $folder . '/' . $this->_router->getAction() .
             '.mobile.' . $this->_viewExt;
            //Device path
            $devicePath = $path = $folder . '/' .
             $this->_router->getAction() . '.' . $application->detector->device .
             '.' . $this->_viewExt;
            if (is_file($devicePath))
                return $devicePath;
            if (is_file($mobilePath))
                return $mobilePath;
        }
        return false;
    }
}